package ca.etsmtl.gti785;

import java.io.IOException;
import java.util.Observable;
import java.util.Observer;
import javax.inject.Singleton;
import ca.etsmtl.gti785.util.*;

/**
 * This class is a singleton who acts for the server. It contains the running threads (Watch Directory, Socket Listener) of the server.
 * @author Étienne Faucher
 */
@Singleton
public class Server extends Observable implements Observer 
{
	private static Server _instance;
	private static WatchDir _watchDir;
	private static SocketListener _socketListener;
	
	public static Server getInstanceOf()
	{
		if(_instance == null)
		{
			_instance = new Server();
		}
		
		return _instance;
	}
	
	private Server()
	{
		try 
		{
			_watchDir = WatchDir.Create(Constants.Shared_Folder, true);
			_watchDir.addObserver(this);

		} 
		catch (IOException e) 
		{
			System.out.println("Can't create server: the shared folder path doesn't exist!");
			e.printStackTrace();
		}

		_socketListener = new SocketListener();
		_socketListener.Start();
		
		System.out.println("Server created!");
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		setChanged();
		notifyObservers(arg1);
	}
}
